package com.itsm.example.incident.flow.action;

import com.itsm.example.audit.pojo.context.AuditContext;
import com.itsm.example.audit.pojo.domain.AuditDO;
import com.itsm.example.incident.bo.Incident;
import com.itsm.example.incident.flow.bo.IncidentContext;
import com.itsm.example.incident.flow.bo.IncidentState;
import com.itsm.example.incident.flow.config.IncidentMachine;
import com.itsm.example.incident.mapper.IncidentMapper;
import com.itsm.example.incident.service.IncidentService;
import com.itsm.example.statemachine.bo.ItsmContext;
import com.itsm.example.statemachine.bo.ItsmEvent;
import com.itsm.example.statemachine.bo.ItsmState;
import com.itsm.statemachine.Action;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class NewIncidentAction implements Action<ItsmState, ItsmEvent, ItsmContext> {

    @Autowired
    @Lazy
    private IncidentService incidentService;

    @Override
    public void execute(ItsmState from, ItsmState to, ItsmEvent event, ItsmContext context) {
        log.info("新建一个工单");
        IncidentContext incidentContext = (IncidentContext) context;
        Incident incident = incidentContext.getIncident() == null ? new Incident() : incidentContext.getIncident();
        incident.setIncidentLevel(1);
        incident.setIncNo("testNo");
        incident.setFlowManageUserName("lipeng");
        incident.setIncTopic("test topic");
        incident.setIncContent("test incident content");
        incident.setInstId(context.getInstId());
        incident.setIncidentState(to.getValue());
        incidentService.saveOrUpdate(incident);
        incidentContext.setId(incident.getId());
        incidentContext.setIncident(incident);
    }
}
